import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from 'src/environments/environment';
import {Observable} from 'rxjs';
import {FrozenConfig} from '../../entity/eureka-manage/frozen-config';

const SERVICE_BASE = `${environment.backendServerBaseAddress}/eureka/manage/frozen`;

@Injectable({
  providedIn: 'root'
})
export class EurekaFrozenService {

  constructor(private http: HttpClient) { }

  /**
   * 获取所有的冻结配置
   */
  public listFrozenConfig(): Observable<FrozenConfig[]> {
    return this.http.get<FrozenConfig[]>(`${SERVICE_BASE}/config`);
  }

  /**
   * 根据eureka代码查询冻结配置
   * @param eurekaCode eureka代码
   */
  public getConfigByEurekaCode(eurekaCode: string): Observable<FrozenConfig> {
    return this.http.get<FrozenConfig>(`${SERVICE_BASE}/config/${eurekaCode}`);
  }

  /**
   * 保存冻结配置
   *
   * @param frozenConfig 冻结配置
   * @return void
   */
  public saveFrozenConfig(frozenConfig: FrozenConfig): Observable<void> {
    return this.http.post<void>(`${SERVICE_BASE}/config`, frozenConfig);
  }

  /**
   * 根据eureka代码移除冻结配置
   *
   * @param eurekaCode eureka代码
   * @return void
   */
  public removeFrozenConfigByCode(eurekaCode: string): Observable<void> {
    return this.http.delete<void>(`${SERVICE_BASE}/config/${eurekaCode}`);
  }
}
